<template>
  <div class="goods-comment">
    <!-- 评价头部 -->
    <div class="head" v-if="commentInfo">
      <div class="data">
        <p><span>{{ commentInfo.salesCount }}</span><span>人购买</span></p>
        <p><span>{{ commentInfo.praisePercent }}</span><span>好评率</span></p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a @click="changeTag(index)" v-for="(i, index) in commentInfo.tags" :key="index" href="javascript:;"
            :class="{ active: currentTagIndex === index }">{{ i.title }}（{{ i.tagCount }}）</a>
        </div>
      </div>
    </div>
    <div class="sort" v-if="commentInfo">
      <span>排序：</span>
      <a  @click="changeSort(null)" :class="{active:reqParams.sortField===null}" href="javascript:;">默认</a>
      <a @click="changeSort('createTime')"  :class="{active:reqParams.sortField==='createTime'}" href="javascript:;">最新</a>
      <a @click="changeSort('praiseCount')"  :class="{active:reqParams.sortField==='praiseCount'}" href="javascript:;">最热</a>
    </div>
    <!-- 评价列表 -->
    <div class="list" v-if="commentList">
      <div class="item" v-for="item in commentList" :key="item.id">
        <div class="user">
          <img :src="item.member.avatar" alt="">
          <span>{{ formatName(item.member.nickname) }}</span>
        </div>
        <div class="body">
          <div class="score">
            <i v-for="i in item.score" :key="i" class="iconfont icon-wjx01"></i>

            <i v-for="i in (5-item.score)" :key="i" class="iconfont icon-wjx02"></i>
            <span class="attr">{{fotmatSpecs(item.orderInfo.specs)}}</span>
          </div>
          <div class="text">{{ item.content }}</div>
          <!-- 评论图片组件 -->
          <goods-comment-image v-if="item.pictures.length" :pictures="item.pictures"></goods-comment-image>
          <div class="time">
            <span>{{ item.createTime }}</span>
            <span class="zan"><i class="iconfont icon-dianzan"></i>{{ item.praiseCount }}</span>
          </div>
        </div>
      </div>
    </div>
    <!-- 分页组件 -->
    <xtx-pagination v-if="total" @change="change" :total="total" :currPage="reqParams.page" :pageSize="reqParams.pageSize"></xtx-pagination>
  </div>
</template>

<script setup >
import { getGoodsComment, getCommentList } from '@/api/product'
import { useRoute } from 'vue-router'
import GoodsCommentImage from './goods-comment-image.vue'
const route = useRoute()
const id = route.params.id
const commentInfo = ref(null)
getGoodsComment(id).then(({ result }) => {
  // 修改数据列表追加  全部评价 有图 两项
  result.tags.unshift({ title: '有图', tagCount: result.hasPictureCount, type: 'img' })
  result.tags.unshift({ title: '全部评价', tagCount: result.evaluateCount, type: 'all' })
  commentInfo.value = result
})
// 激活tags
const currentTagIndex = ref(0) // 默认激活全部评论
// 准备筛选条件数据
const reqParams = reactive(
  {
    page: 1,
    pageSize: 10,
    hasPicture: null,
    tag: null,
    sortField: null // 可选值范围[praiseCount,createTime] 热度，最新评价
  }
)
const changeSort = (sortField) => { // 点击排序
  reqParams.sortField = sortField
  reqParams.page = 1 // 重置页码
}
const changeTag = (index) => {
  currentTagIndex.value = index
  // 修改对应参数// 全部=>不修改 ， 有图=>修改
  const tag = commentInfo.value.tags[index]
  if (tag.type === 'all') {
    reqParams.tag = null
    reqParams.hasPicture = null
  } else if (tag.type === 'img') {
    reqParams.tag = null
    reqParams.hasPicture = true
  } else {
    reqParams.hasPicture = null
    reqParams.tag = tag.title
  }
  reqParams.page = 1 // 重置页码
}
const commentList = ref([])
// 总数据
const total = ref(0)
watch(reqParams, () => {
  // 每次条件发生改变重置页码
  // reqParams.page = 1 不建议在watch中重置，如果页码变化会再次触发watch
  getCommentList(id, reqParams).then(({ result }) => {
    commentList.value = result.items
    total.value = result.counts
  })
}, {
  immediate: true
})
// 定义转换渲染数据的函数(相当于vue2的filter)
const fotmatSpecs = (specs) => {
  return specs.reduce((p, c) => `${p} ${c.name}:${c.nameValue}`.trim(), '')
}
// 名子
const formatName = (name) => {
  return name[0] + '****' + name[name.length - 1]
}
const change = (page) => {
  reqParams.page = page
}
</script>

<style scoped lang='less'>
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;

    .data {
      width: 340px;
      display: flex;
      padding: 20px;

      p {
        flex: 1;
        text-align: center;

        span {
          display: block;

          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }

          &:last-child {
            color: #999;
          }
        }
      }
    }

    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;

      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }

      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;

        >a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;

          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }

          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }

  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;

    >span {
      margin-left: 20px;
    }

    >a {
      margin-left: 30px;

      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
}

.list {
  padding: 0 20px;

  .item {
    display: flex;
    padding: 25px 10px;
    border-bottom: 1px solid #f5f5f5;

    .user {
      width: 160px;

      img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        overflow: hidden;
      }

      span {
        padding-left: 10px;
        color: #666;
      }
    }

    .body {
      flex: 1;

      .score {
        line-height: 40px;

        .iconfont {
          color: #ff9240;
          padding-right: 3px;
        }

        .attr {
          padding-left: 10px;
          color: #666;
        }
      }
    }

    .text {
      color: #666;
      line-height: 24px;
    }

    .time {
      color: #999;
      display: flex;
      justify-content: space-between;
      margin-top: 5px;
    }
  }
}
</style>
